#ifndef WRK_H
#define WRK_H

#include "config.h"
#include <pthread.h>
#include <inttypes.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <luajit-2.0/lua.h>

#include "stats.h"
#include "ae.h"
#include "http_parser.h"

#define RECVBUF  8192 //8k

#define MAX_THREAD_RATE_S   10000000
#define SOCKET_TIMEOUT_MS   2000
#define RECORD_INTERVAL_MS  100

extern const char *VERSION;//版本

//线程
typedef struct {
    pthread_t thread;//线程id
    aeEventLoop *loop;//ae事件循环
    struct addrinfo *addr;//地址信息
    uint64_t connections;//连接数
    uint64_t complete;//完成数
    uint64_t requests;//请求数
    uint64_t bytes;//字节数
    uint64_t start;//开始时间
    lua_State *L;//lua指针
    errors errors;//错误计数
    struct connection *cs;//连接
} thread;

//缓冲区
typedef struct {
    char  *buffer; //缓冲区
    size_t length; //缓冲区长度
    char  *cursor; //游标
} buffer;

//连接
typedef struct connection {
    thread *thread;
    http_parser parser;
    enum {
        FIELD, VALUE
    } state;
    int fd; //文件描述符
    SSL *ssl;//ssl
    bool delayed;//延时
    uint64_t start;//启动时间
    char *request;//请求
    size_t length;//长度
    size_t written;//已经写的数据长度
    uint64_t pending;//挂起
    buffer headers;//头
    buffer body;//体
    char buf[RECVBUF];//接收缓冲区
} connection;

#endif /* WRK_H */
